//
//  LWVisterView.swift
//  ios8-01 weibo框架搭建
//
//  Created by blue on 16/2/27.
//  Copyright © 2016年 honeywell. All rights reserved.
//

import UIKit

//1.定义代理
protocol  LWVisterViewDelegate :NSObjectProtocol{
    
    //定义代理方法－－注册点击事件－－登陆点击事件
    func  registerBtnClick()
    
    func  loginBtnClick()
    
    
}

class LWVisterView: UIView {
    
    //2.定义代理属性
    weak  var  registerDelegate :LWVisterViewDelegate?
    

    //添加子空间到父视图--代码创建调用这个方法
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        //设置视图的背景颜色让遮罩显示出来
//       self.backgroundColor = UIColor(red: 237.0/255.0, green: 237.0/255.0, blue: 237.0/255.0, alpha: 1)
     
                //1.添加子控件
//        self.prepareUI()
        
        //2.给子空间添加约束
        
//         homeView.translatesAutoresizingMaskIntoConstraints = false
//         iconView.translatesAutoresizingMaskIntoConstraints = false
//         messageLable.translatesAutoresizingMaskIntoConstraints = false
//         registerBtn.translatesAutoresizingMaskIntoConstraints = false
//         loginBtn.translatesAutoresizingMaskIntoConstraints = false
//         coverView.translatesAutoresizingMaskIntoConstraints = false
        
        self.prepareUIforConstrait()
        
        
    }
    
    //xib
    required init?(coder aDecoder: NSCoder) {
        
        super.init(coder: aDecoder)
        
    }
    
    override func layoutSubviews() {
        
        //self.backgroundColor = UIColor.redColor()
              self.backgroundColor = UIColor(red: 237.0/255.0, green: 237.0/255.0, blue: 237.0/255.0, alpha: 1)

        
    }
    
    
    //MARK  设置每个控制器的访客视图的内容==转轮图片 消息标签
    func  setupVistorView(imageName:String, title: String){
        
        //隐藏房子
        self.homeView.hidden = true
        //替换转盘的图片
        self.iconView.image = UIImage(named: imageName)
        //设置消息标签的内容
        self.messageLable.text = title
        
        sendSubviewToBack(self.coverView)
        
        
    }
    
    //MARK  添加子控件
    func  prepareUI(){
        
//        //添加有顺序
//        self.addSubview(iconView)
//        self.addSubview(coverView)
//        self.addSubview(homeView)
//        self.addSubview(messageLable)
//        self.addSubview(registerBtn)
//       // print("注册按钮：\(registerBtn)")
//        self.addSubview(loginBtn)
        
    }
    
    //添加子空间的约束＝＝设置frame
    func  prepareUIforConstrait(){
        //添加有顺序
        self.addSubview(iconView)
        self.addSubview(coverView)
        self.addSubview(homeView)
        self.addSubview(messageLable)
        self.addSubview(registerBtn)
        // print("注册按钮：\(registerBtn)")
        self.addSubview(loginBtn)
        
        
        homeView.translatesAutoresizingMaskIntoConstraints = false
        iconView.translatesAutoresizingMaskIntoConstraints = false
        messageLable.translatesAutoresizingMaskIntoConstraints = false
        registerBtn.translatesAutoresizingMaskIntoConstraints = false
        loginBtn.translatesAutoresizingMaskIntoConstraints = false
        coverView.translatesAutoresizingMaskIntoConstraints = false
        //遮罩
        
        let covert = NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0)
        self.addConstraint(covert)
        
        let coverb = NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: registerBtn, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0)
        self.addConstraint(coverb)

        let coverl = NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0)
        self.addConstraint(coverl)

        let coverr = NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0)
        self.addConstraint(coverr)

        
        //房子的中心x
        let  top0 = NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)
        self.addConstraint(top0)
        
        
        //房子的中心y
        let  bottom0 = NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0)
        self.addConstraint(bottom0)


        
        //转轮的中心x
        let  top1 = NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)
        self.addConstraint(top1)
        
        ///转轮的中心y
        let  bottom1 = NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0)
        self.addConstraint(bottom1)
        
        
        ///消息标签的中心x
        let  bottom2 = NSLayoutConstraint(item: messageLable, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)
        self.addConstraint(bottom2)
        
        ///消息标签顶部＝＝转轮的底部＋16
        let  bottom2_1 = NSLayoutConstraint(item: messageLable, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 30)
        self.addConstraint(bottom2_1)

        
        
        
        
        ///消息标签的宽度＝＝240
        let  w = NSLayoutConstraint(item: messageLable, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 240)
        self.addConstraint(w)
        
        

        //注册按钮的顶部==消息标签的底部距离16
        let  top3 = NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLable, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16)
        self.addConstraint(top3)
        
        
        ///注册按钮的左边=＝消息标签的左边
        let  left3 = NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: messageLable, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0)
        self.addConstraint(left3)
        
        
        ///注册按钮的宽度＝＝100
        let  right3 = NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100)
        self.addConstraint(right3)

        
        //注册按钮的高度＝＝35
        let  height1 = NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35)
        self.addConstraint(height1)
        
        
        
        ///登录按钮顶部＝＝注册按钮的顶部
        let  bottom4 = NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: registerBtn, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0)
        self.addConstraint(bottom4)
        
        ///登录按钮的右边＝＝消息标签的右边
        let  left4 = NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: messageLable, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0)
        self.addConstraint(left4)
        
        
        ///登录按钮的宽度＝＝注册按钮的宽度
        let  right4 = NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: registerBtn, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0)
        self.addConstraint(right4)

        ///登录按钮的高度＝＝注册按钮的高度
        let  right5 = NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: registerBtn, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: 0)
        self.addConstraint(right5)
        
 
    }
    //MARK:- 懒加载
    
    
    //小房子
    lazy  var  homeView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //转盘
    lazy  var  iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //消息标签
    lazy  var  messageLable: UILabel = {
        
        let    lab = UILabel()
        lab.numberOfLines = 2
        lab.text = "关注一些人，关注一些事"
        lab.font = UIFont.systemFontOfSize(18)
        
        
        return  lab
    }()
    
    //注册按钮
    lazy  var  registerBtn :UIButton = {
        
        let  btn = UIButton()
        
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        //设置按钮的背景图片
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        //设置按钮的文本内容
        btn.setTitle("注册" ,forState: UIControlState.Normal)
        
        //给按钮添加点击事件－点击跳转到注册界面
        btn.addTarget(self, action: "jumpToRegisterVc", forControlEvents: UIControlEvents.TouchUpInside)
        
        print("注册按钮创建好")
        return btn
    }()
    
    //登录按钮
    lazy  var  loginBtn :UIButton = {
        
        let  btn = UIButton()
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)

        //设置按钮的背景图片
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        //设置按钮的文本内容
        btn.titleLabel?.text = "denf"
        btn.setTitle("登陆" ,forState: UIControlState.Normal)
        
        //给按钮添加点击事件－点击跳转到注册界面
        btn.addTarget(self, action: "jumpToLoginVc", forControlEvents: UIControlEvents.TouchUpInside)
        
        print("登陆按钮创建好")
        return btn

    }()
    
    //MARK:-  设置遮罩
    lazy var coverView : UIImageView = {
        
       let  cView = UIImageView()
        
        cView.image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        
        //cView.sizeToFit()
        //sendSubviewToBack(cView)
        
        return  cView
        
    }()
    
    
    //MARK: 点击登陆事件
    func  jumpToLoginVc(){
        
        //print("登陆")
        
        //3.判断代理的响应
     
        registerDelegate?.loginBtnClick()
        
    }
    //MARK点击注册事件
    func jumpToRegisterVc(){
        
        //print("注册")
        //3.判断代理的响应
        registerDelegate!.registerBtnClick()
    }
    
    //MARK 转轮动画
    func iconViewAnimation() {
        
        //创建动画
        let  animation = CABasicAnimation(keyPath: "transform.rotation")
        //配置动画
        animation.repeatCount = MAXFLOAT
        animation.duration = 20
        animation.toValue = 2 * M_PI
        animation.removedOnCompletion = false
        
        //添加动画
        iconView.layer.addAnimation(animation, forKey: nil)
        
    }
    


}
